package com.qike.duola.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qike.duola.domain.DuolaGoods;
import com.qike.duola.domain.DuolaGoodsExample;
import com.qike.duola.dto.*;
import com.qike.duola.enums.ErrorEnums;
import com.qike.duola.enums.GoodsTypeEnums;
import com.qike.duola.enums.StatusEnums;
import com.qike.duola.mapper.DuolaGoodsMapper;
import com.qike.duola.service.DuolaDeptService;
import com.qike.duola.service.DuolaGoodsService;
import com.qike.duola.vo.*;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/2 15:10
 */
@Service
public class DuolaGoodsServiceImpl implements DuolaGoodsService {
    @Autowired
    private DuolaGoodsMapper duolaGoodsMapper;
    private static final Logger logger = LoggerFactory.getLogger(DuolaGoodsServiceImpl.class);

    /**
     * 添加物料
     *
     * @param goodsAddDto
     */
    @Override
    public void GoodsAdd(GoodsAddDto goodsAddDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"添加物料"+ JSON.toJSON(goodsAddDto));
        this.findSame(goodsAddDto.getGoodsName(),goodsAddDto.getGoodsType(),null);
        DuolaGoods duolaGoods = new DuolaGoods();
        duolaGoods.setAddTime(LocalDateTime.now());
        duolaGoods.setAddUid(userId);
        duolaGoods.setStatus(StatusEnums.NO.getEcode());
        duolaGoods.setGoodsType(goodsAddDto.getGoodsType());
        duolaGoods.setGoodsUnit(goodsAddDto.getGoodsUnit());
        duolaGoods.setGoodsName(goodsAddDto.getGoodsName());
        duolaGoods.setGoodsPrice(goodsAddDto.getGoodsPrice());
        duolaGoodsMapper.insertSelective(duolaGoods);
    }

    /**
     * 物料列表
     *
     * @param goodsListDto
     * @return
     */
    @Override
    public GoodsListVo goodsList(GoodsListDto goodsListDto) {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"物料列表"+ JSON.toJSON(goodsListDto));
        GoodsListVo goodsListVo = new GoodsListVo();
        List<GoodsList> list = new ArrayList<>();
        DuolaGoodsExample duolaGoodsExample = new DuolaGoodsExample();
        DuolaGoodsExample.Criteria criteria = duolaGoodsExample.createCriteria();
        if (!StringUtils.isEmpty(goodsListDto.getGoodsName())){
            criteria.andGoodsNameLike(goodsListDto.getGoodsName());
        }
        if (goodsListDto.getGoodsType()!=null){
            criteria.andGoodsTypeEqualTo(goodsListDto.getGoodsType());
        }
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode());
        Page page = PageHelper.startPage(goodsListDto.getStartPage(), goodsListDto.getPageSize());
        duolaGoodsExample.setOrderByClause("add_time desc");
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(duolaGoodsExample);
        for (DuolaGoods duolaGood : duolaGoods) {
            GoodsList goodsList = new GoodsList();
            goodsList.setId(duolaGood.getId());
            goodsList.setGoodsName(duolaGood.getGoodsName());
            goodsList.setGoodsPrice(duolaGood.getGoodsPrice());
            goodsList.setGoodsType(duolaGood.getGoodsType());
            goodsList.setGoodsTypeStr(GoodsTypeEnums.statOf(duolaGood.getGoodsType()).getEmsg());
            goodsList.setGoodsUnit(duolaGood.getGoodsUnit());
            list.add(goodsList);
        }
        goodsListVo.setList(list);
        goodsListVo.setTotal(page.getTotal());
        return goodsListVo;
    }

    /**
     * 删除物料
     *
     * @param deptDelDto
     */
    @Override
    public void goodsDel(DeptDelDto deptDelDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"删除物料"+ JSON.toJSON(deptDelDto));
        DuolaGoods duolaGoods = duolaGoodsMapper.selectByPrimaryKey(deptDelDto.getId());
        if (duolaGoods==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        duolaGoods.setStatus(StatusEnums.YES.getEcode());
        duolaGoods.setUpdateTime(LocalDateTime.now());
        duolaGoods.setUpdateId(userId);
        duolaGoodsMapper.updateByPrimaryKeySelective(duolaGoods);
    }

    /**
     * 物料编辑
     *
     * @param goodsEditDto
     */
    @Override
    public void goodsEdit(GoodsEditDto goodsEditDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"物料编辑"+ JSON.toJSON(goodsEditDto));
        DuolaGoods duolaGoods = duolaGoodsMapper.selectByPrimaryKey(goodsEditDto.getId());
        if (duolaGoods==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        this.findSame(goodsEditDto.getGoodsName(),goodsEditDto.getGoodsType(),goodsEditDto.getId());
        duolaGoods.setUpdateId(userId);
        duolaGoods.setUpdateTime(LocalDateTime.now());
        duolaGoods.setGoodsName(goodsEditDto.getGoodsName());
        duolaGoods.setGoodsPrice(goodsEditDto.getGoodsPrice());
        duolaGoods.setGoodsType(goodsEditDto.getGoodsType());
        duolaGoods.setGoodsUnit(goodsEditDto.getGoodsUnit());
        duolaGoodsMapper.updateByPrimaryKeySelective(duolaGoods);
    }

    /**
     * 物料下拉
     *
     * @param goodsType
     * @return
     */
    @Override
    public List<GoodsPullListVo> goodsPullList(Integer goodsType) {
        List<GoodsPullListVo> listVos = new ArrayList<>();
        DuolaGoodsExample example = new DuolaGoodsExample();
        DuolaGoodsExample.Criteria criteria = example.createCriteria();
        if (goodsType!=null){
            criteria.andGoodsTypeEqualTo(goodsType);
        }
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(example);
        for (DuolaGoods duolaGood : duolaGoods) {
            GoodsPullListVo vo = new GoodsPullListVo();
            vo.setGoodsName(duolaGood.getGoodsName());
            vo.setGoodsType(duolaGood.getGoodsType());
            vo.setId(duolaGood.getId());
            vo.setGoodsUnit(duolaGood.getGoodsUnit());
            vo.setGoodsPrice(duolaGood.getGoodsPrice());
            listVos.add(vo);
        }
        return listVos;
    }

    /**
     * 通过不选择的物料类型物料下拉
     *
     * @param goodsType
     * @return
     */
    @Override
    public List<GoodsPullListVo> goodsPullListWithOut(Integer goodsType) {
        List<GoodsPullListVo> listVos = new ArrayList<>();
        DuolaGoodsExample example = new DuolaGoodsExample();
        DuolaGoodsExample.Criteria criteria = example.createCriteria();
        if (goodsType!=null){
            criteria.andGoodsTypeNotEqualTo(goodsType);
        }
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(example);
        for (DuolaGoods duolaGood : duolaGoods) {
            GoodsPullListVo vo = new GoodsPullListVo();
            vo.setGoodsName(duolaGood.getGoodsName());
            vo.setGoodsType(duolaGood.getGoodsType());
            vo.setId(duolaGood.getId());
            vo.setGoodsUnit(duolaGood.getGoodsUnit());
            vo.setGoodsPrice(duolaGood.getGoodsPrice());
            listVos.add(vo);
        }
        return listVos;
    }


    private  void findSame(String name,Integer goodsType,Integer id) throws Exception {
        DuolaGoodsExample example = new DuolaGoodsExample();
        DuolaGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode()).andGoodsNameEqualTo(name).andGoodsTypeEqualTo(goodsType);
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(example);
        if (duolaGoods.size()>0){
            if (id!=null&&!duolaGoods.get(0).getId().equals(id)){
                throw new Exception(ErrorEnums.SAME_GOODS_NAME.getEmsg());
            }
        }

    }

}

